package triggerhelper.helperClasses;

import java.util.ArrayList;
import javax.swing.*;
import triggerhelper.TextFrame;
import triggerhelper.controllers.MenuBarController;
import triggerhelper.dataHolders.*;
import triggerhelper.triggerCreator;

public class TriggerCompiler {

    private static final String END_TRIGGER = "\n}\n\n//--------------------------\\\\\n\n";

    public static void compileProject(triggerCreator parent, TriggerProject tp){
        MenuBarController mbController = parent.getMenuBarController();
        //if not saved, ask to save before compiling
        int option = mbController.checkIfSaved();
        if (option != 2 && option != JOptionPane.CLOSED_OPTION) {

            ArrayList<SimpleTrigger> triggers = tp.getTriggers();
            if(triggers.isEmpty()){
                JOptionPane.showMessageDialog(parent, "I'm sorry, there are no triggers for this project."
                        + "\nYou must have at least one trigger to compile text output");
                return;
            }
            TextFrame display = new TextFrame();
            int x = parent.getX() + parent.getWidth();
            int y = parent.getY();
            display.setLocation(x, y);
            display.setTitle("Project Output");
            display.setlMainTitle("Project Text");
            display.setVisible(true);
            for(SimpleTrigger t : triggers){
                if(!compileTrigger(display, t)){
                    break;
                }
            }
            display.gettOutput().setCaretPosition(0);
        }
    }

    public static boolean compileTrigger(TextFrame display, SimpleTrigger aTrigger){
        JTextArea output = display.gettOutput();

        if(!aTrigger.isSimple()){
            boolean done = false;
            String trigger = ((Trigger)aTrigger).getConditions() + "\n\n" + ((Trigger)aTrigger).getActions();
            int counter = 0;
            String result = "";

            ArrayList<Variable> variables = ((Trigger)aTrigger).getVariables();
            int numIterations = ((Trigger)aTrigger).getNumIterations();

            if(variables.isEmpty()){
                JOptionPane.showMessageDialog(display, "I'm sorry, there are no variables in trigger " +
                        aTrigger.getName() + ".\nAborting compilation of trigger.");
                return false;
            }

            while(!done){
                String tempT = trigger;
                for(Variable var : variables){
                    String rName = "[" + var.getName() + "]";
                    int i = var.getIterate();
                    if(var.isNumber()){
                        int dif = var.getMaxValue() - var.getMinValue() + 1;
                        int min = var.getMinValue();
                        tempT = tempT.replace(rName, Integer.toString( ((counter / i) % dif) + min));
                    }else{//var has string instances
                        DefaultListModel instances = var.getInstances();
                        int dif = instances.size();
    //                    System.out.println("size : " + dif + "\tcounter : " + counter + "\ti val : " + i + "\thuh : " + ((counter / i) % dif)
    //                                + "\tinstance : " + instances.get((counter / i) % dif));
                        tempT = tempT.replace(rName, (String)instances.getElementAt((counter / i)% dif));
                    }
                }
                result += tempT + END_TRIGGER;
                counter++;
                if(counter % 100 == 0){
                    output.append(result);
                    result = "";
                }
                if(counter == numIterations){
                    done = true;
                }
            }
            if(counter % 100 != 0){
                output.append(result);
            }
        }else{
            output.append(aTrigger.getFullTrigger() + END_TRIGGER);
        }
        return true;
    }
}
